
var CurrentUser=getUserID();

function InsertQuestionItem(){	
	var QuestionID=gup("QuestionID");
	var QuestionType=swapTypeKey(gup("type"));
	var numberObjects=gup("numberObjects");
	var i=0;
	switch (QuestionType){
	case "1":
		for (i=1;i<=numberObjects;i++){
			if (InsertQuestionItem_Muilt(QuestionID,i)=="false"){
				alert("Got problem at item "+i+ ".Pls check again!");
				return;
			}else{
				alert("OK at : "+i);
			}				
		}
		break;
	case "2":
		for (i=1;i<=numberObjects;i++){
			if (InsertQuestionItem_FillBlank(QuestionID,i)=="false"){
				alert("Got problem at item "+i+ ".Pls check again!");
				return;
			}else{
				alert("OK at : "+i);
			}
				
		}
		break;
	case "3":
		for (i=1;i<=numberObjects;i++){
			if (InsertQuestionItem_Matching(QuestionID,i)=="false"){
				alert("Got problem at item "+i+ ".Pls check again!");
				return;
			}else{
				alert("OK at : "+i);
			}
				
		}
		break;
	default:		
			if (InsertQuestionItem_Fulltext(QuestionID)=="false"){
				alert("Got problem.Pls check again!");
				return;
			}else{
				alert("OK");
			}
		break;
	}
}

function InsertQuestionItem_Muilt(questionID,i)
{
	//create/{questionID}/{answerDesc}/{answerValue}/{solution
	var answerDesc =document.getElementById("txtAnswer"+i+"").value;
	var answerValue = document.getElementById("txtAnswer"+i+"").value;
	var solution;
	if (document.getElementById("txtRadio"+i+"").checked==true)
		{solution=true;}
	else{
		solution=false;}
	//alert(solution);
	
	if(answerDesc == "" || answerValue == "" )	
	{
		alert("Please fill all the informations");
		return;
	}
	var urlPath = "./rest/answer/create/"+questionID+"/"+answerDesc+"/"+answerValue+"/"+solution+"/";
	//alert(urlPath);
	objPost.onreadystatechange = function(){
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	return "ok";		        	
		        }
		        else{
		        	return "false";
		        }	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}
function UpdateQuestionItem_Muilt(answerID)
{
	///update/{answerID}/{questionID}/{answerDesc}/{answerValue}/{solution}
	var questionID=gup("QuestionID");
	var answerDesc =document.getElementById("txtAnswer"+answerID+"").value;
	var answerValue = document.getElementById("txtAnswer"+answerID+"").value;
	var solution;
	if (document.getElementById("txtRadio"+answerID+"").checked==true)
		{solution=true;}
	else{
		solution=false;}
	//alert(answerValue);
	
	if(answerDesc == "" || answerValue == "" )	
	{
		alert("Please fill all the informations");
		return;
	}
	var urlPath = "./rest/answer/update/"+answerID+"/"+questionID+"/"+answerDesc+"/"+answerValue+"/"+solution+"/";
	//alert(urlPath);
	objPost.onreadystatechange = function(){
		//alert(objPost.status); 
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	alert("Successfull!");		        	
		        }
		        else{
		        	alert( "fail, pls try again!");
		        	}	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}

function DeleteAnswerItem(AnswerID){
	if (window.confirm("Are you sure to delete Answer have ID: "+AnswerID+" ?")){
	var urlPath = "./rest/answer/delete/"+AnswerID+"/";
	obj.onreadystatechange = function(){		
		if (obj.readyState == 4) {
			//alert(urlPath);
		        if (obj.status == 200) {
		        	location.reload(); 
		        }
		        else{alert("Error: Pls try again!"+obj.responseText+" --- ");}	
		   }
		
	};		
	obj.open("GET",urlPath,true);
	obj.send(null);}
	else
		return;
}	
function InsertQuestionItem_FillBlank(questionID,i)
{
	//create/{questionID}/{answerDesc}/{answerValue}/{solution
	var answerDesc =document.getElementById("txtFill"+i+"").value;
	var answerValue = document.getElementById("txtAnswer"+i+"").value;
	var solution=false;	
	if(answerDesc == "" || answerValue == "" )	
	{
		alert("Please fill all the informations");
		return;
	}
	var urlPath = "./rest/answer/create/"+questionID+"/"+answerDesc+"/"+answerValue+"/"+solution+"/";
	//alert(urlPath);
	objPost.onreadystatechange = function(){
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	return "ok";		        	
		        }
		        else{
		        	return "false";
		        }	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}
function UpdateQuestionItem_FillBlank(answerID)
{	
	///update/{answerID}/{questionID}/{answerDesc}/{answerValue}/{solution}
	var questionID=gup("QuestionID");
	var answerDesc =document.getElementById("txtFill"+answerID+"").value;
	var answerValue = document.getElementById("txtAnswer"+answerID+"").value;
	var solution=false;
	if(answerDesc == "" || answerValue == "" )	
	{
		alert("Please fill all the informations");
		return;
	}
	var urlPath = "./rest/answer/update/"+answerID+"/"+questionID+"/"+answerDesc+"/"+answerValue+"/"+solution+"/";
	//alert(urlPath);
	objPost.onreadystatechange = function(){
		//alert(objPost.status); 
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	alert("Successfull!");		        	
		        }
		        else{
		        	alert( "fail, pls try again!");
		        	}	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}

function InsertQuestionItem_Matching(questionID,i)
{
	//create/{questionID}/{answerDesc}/{answerValue}/{solution
	var answerDesc =document.getElementById("txtMatching"+i+"").value;
	var answerValue = document.getElementById("txtAnswer"+i+"").value;
	var solution=false;	
	if(answerDesc == "" || answerValue == "" )	
	{
		alert("Please fill all the informations");
		return;
	}
	var urlPath = "./rest/answer/create/"+questionID+"/"+answerDesc+"/"+answerValue+"/"+solution+"/";
	//alert(urlPath);
	objPost.onreadystatechange = function(){
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	return "ok";		        	
		        }
		        else{
		        	return "false";
		        }	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}
function UpdateQuestionItem_Matching(answerID)
{	
	///update/{answerID}/{questionID}/{answerDesc}/{answerValue}/{solution}
	var questionID=gup("QuestionID");
	var answerDesc =document.getElementById("txtMatching"+answerID+"").value;
	var answerValue = document.getElementById("txtAnswer"+answerID+"").value;
	var solution=false;
	if(answerDesc == "" || answerValue == "" )	
	{
		alert("Please fill all the informations");
		return;
	}
	var urlPath = "./rest/answer/update/"+answerID+"/"+questionID+"/"+answerDesc+"/"+answerValue+"/"+solution+"/";
	//alert(urlPath);
	objPost.onreadystatechange = function(){
		//alert(objPost.status); 
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	alert("Successfull!");		        	
		        }
		        else{
		        	alert( "fail, pls try again!");
		        	}	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}


function InsertQuestionItem_Fulltext(questionID)
{
	//create/{questionID}/{answerDesc}/{answerValue}/{solution
	var answerDesc =document.getElementById("txtQuestionDesc").value;
	//alert(document.getElementById("txtQuestionDesc").innerHTML);
	var solution=false;	
	if(answerDesc == ""  )	
	{
		alert("Please fill all the informations");
		return;
	}
	var urlPath = "./rest/answer/create/"+questionID+"/"+answerDesc+"/"+answerDesc+"/"+solution+"/";
	//alert(urlPath);
	objPost.onreadystatechange = function(){
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	return "ok";		        	
		        }
		        else{
		        	return "false";
		        }	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}
function UpdateQuestionItem_Fulltext(answerID)
{	
	///update/{answerID}/{questionID}/{answerDesc}/{answerValue}/{solution}
	var questionID=gup("QuestionID");
	var answerDesc =document.getElementById("txtQuestionDesc").innerHTML;
	//alert(document.getElementById("txtQuestionDesc").innerHTML);
	var solution=false;
	if(answerDesc == "" )	
	{
		alert("Please fill all the informations");
		return;
	}
	var urlPath = "./rest/answer/update/"+answerID+"/"+questionID+"/"+answerDesc+"/"+answerDesc+"/"+solution+"/";
	//alert(urlPath);
	objPost.onreadystatechange = function(){
		//alert(objPost.status); 
		if (objPost.readyState == 4) {
		        if (objPost.status == 200) {
		        	alert("Successfull!");		        	
		        }
		        else{
		        	alert( "fail, pls try again!");
		        	}	
		   }
	};		
	objPost.open("GET",urlPath,true);
	objPost.send(null);
}


